<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="handleAdd(1, '0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id" >
          <el-table-column label="名称" prop="name"/>
          <el-table-column label="标识" prop="code"/>
          <el-table-column label="描述" prop="description"/>
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type===1" size="small" type="text"  @click="handleAdd(1, row.id)">添加权限点</el-button>
              <el-button size="small" type="text">查看</el-button>
              <el-button size="small" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" title="弹层标题" @close="showDialog = false">
      <!-- 表单内容 -->
      <el-form ref="addRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input  v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-value="0"
            inactive-value="1"
            active-text="启用"
            inactive-text="禁用"
            active-color="#13ce66"
            inactive-color="#eee">
          </el-switch>
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary"  @click="handleSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {reqGetPermissionList,reqAddPermission} from '@/api/permission'
import { translateListToTreeData } from '@/utils/index.js'
export default {
  name: 'Permission',
  data(){
    return{
    //控制添加权限弹窗显示与隐藏
    showDialog:false,
     // 权限列表
     list:[],
     //添加权限的表单数据
     formData: {
      enVisible: '0', // 开启
      name: '', // 名称
      code: '', // 权限标识
      description: '', // 描述
      type: '', // 类型 一级或二级部门
      pid: '' // 添加到哪个节点下 二级子部门
    },
    // 表单验证
      rules: {
        name: [
          { required: true, message: '权限点名称不能为空', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '权限点编码不能为空', trigger: ['blur', 'change'] }
        ]
      }
  }
  },
  created(){
   this.loadPermissionList()
  },
  methods: {
    // 添加权限提交表单
    handleAdd (type, pid) {
      // 设置权限的级别
      this.formData.type=type
      // 设置权限的父id
      this.formData.pid=pid
      // 显示弹窗
     this.showDialog=true
    },
    //添加权限,提交表单
    handleSubmit(){
      this.$refs.addRef.validate(async valid=>{
        if(valid){
          try {const ret=await reqAddPermission(this.formData)
          if(ret.code===10000){
            //添加权限成功，关闭弹窗，刷新列表 重置表单
            this.showDialog=false
            this.loadPermissionList()
            this.$refs.addRef.resetFields()
            this.formData=this.$options.data().formData
            this.$message.success('添加权限成功')
          }
          }catch{
            this.$message.error('添加权限失败')
          }
          
        }
      })
    },
    // 获取权限列表
   async loadPermissionList(){
     try{
       const ret=await reqGetPermissionList()
     if (ret.code===10000){
      // this.list=ret.data
      //把列表数据转换为树形数据
      this.list=translateListToTreeData(ret.data,'0')
     }
     }catch{
       this.$message.error("获取权限列表失败")
     }
    }
  }
}
</script>